package org.smoozik.app.batch;

import java.io.IOException;
import java.util.ArrayList;

import org.smoozik.services.musictheory.Chord;
import org.smoozik.services.musictheory.Figure;
import org.smoozik.services.musictheory.Note;
import org.smoozik.services.musictheory.NoteGenerator;
import org.smoozik.services.musictheory.Scale;
import org.smoozik.services.sound.SoundPlayer;

public class FigureGenerator {
	int min;
	int max;

	public static int TYPE_NOTE = 0;
	public static int TYPE_INTERVAL = 3;
	public static int TYPE_CHORD = 2;

	public FigureGenerator(int min, int max) {
		this.min = min;
		this.max = max;
	}

	public void launchGenerator() {
		Thread t = new GeneratorThread();
		t.start();
	}

	public Figure randomGenerate() {
		return randomGenerate(min, max);
	}

	public Figure randomGenerateNote() {
		return randomGenerate(min, max, TYPE_NOTE);
	}

	public Figure randomGenerate(int min, int max) {
		int type = NoteGenerator.getRandomFromBounds(0, 2);

		return randomGenerate(min, max, type);
	}

	public Figure randomGenerate(int min, int max, int type) {
		Figure f = null;

		if (type == 0) {
			f = generateNote();
		} else if (type == 1) {
			f = generateChord();
		} else if (type == 2) {
			f = generateIntervalle();
		}

		return f;
	}

	public void playFigure(Figure f) {

		SoundPlayer sp = new SoundPlayer();
		sp.playFigure(f);
		System.out.println(f);
	}

	public Figure scaleGenerate(Scale s) {
		ArrayList<Note> liste = s.getNotes(0, 20);

		int n = NoteGenerator.getRandomFromBounds(0, liste.size() - 1);
		Note note = liste.get(n);

		return note;
	}

	public Figure generateNote() {
		Note note = NoteGenerator.getRandomNote(min, max, 0);
		return note;
	}

	public Figure generateChord() {

		Chord chord = NoteGenerator.getRandomChord(min, max, 1, 1);
		return chord;
	}

	public Figure generateIntervalle() {

		Chord chord = NoteGenerator.getRandomIntervalle(min, max, 1);

		return chord;
	}

	public static void main(String[] args) throws IOException {
//		FigureGenerator gen = new FigureGenerator(-5, 2);
//		TimeLineManager tm = new TimeLineManager(new SoundPlayer());
//		ArrayList<Token> tokens = new ArrayList<>();
//		Sequence seq = new Sequence(500);
//		Figure f = null;
//
//		seq.setTokens(tokens);
//		tm.setSequence(seq);
//
//		for (int i = 0; i < 13; i++) {
//			f = gen.randomGenerate();
//			tokens.add(new Token(f));
//		}
//
//		seq.setRythmicPattern(new RythmicPattern(), 0);
//		seq.repeat();
//
//		tm.startTimer();
//		try {
//			Thread.sleep(22000);
//		} catch (InterruptedException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		tm.stopTimer();

	}

	class GeneratorThread extends Thread {

		public GeneratorThread() {
			// TODO Auto-generated constructor stub
		}

		public void run() {
			do {
				Figure f = randomGenerate();
				playFigure(f);
				try {
					Thread.sleep(2000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			} while (true);
		}

	}

}
